package com.xmut.mall.store.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xmut.mall.store.utils.RespBean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.store.utils.PageUtils;
import com.xmut.mall.store.utils.Query;

import com.xmut.mall.store.dao.StoreLogisticsDao;
import com.xmut.mall.store.entity.StoreLogisticsEntity;
import com.xmut.mall.store.service.StoreLogisticsService;


@Service("storeLogisticsService")
public class StoreLogisticsServiceImpl extends ServiceImpl<StoreLogisticsDao, StoreLogisticsEntity> implements StoreLogisticsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StoreLogisticsEntity> page = this.page(
                new Query<StoreLogisticsEntity>().getPage(params),
                new QueryWrapper<StoreLogisticsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 添加 店铺-物流公司 关联
     * @param param
     * @return
     */
    @Override
    public RespBean addStoreLogistics(Map<String, Object> param) {
        Long logisticsId = MapUtil.getLong(param,"logisticsId");
        Long storeId = MapUtil.getLong(param, "storeId");

        StoreLogisticsEntity entity = new StoreLogisticsEntity();
        entity.setLogisticsId(logisticsId);
        entity.setStoreId(storeId);
        entity.setCreateTime(new Date());


        boolean save = this.save(entity);
        if (save){
            return RespBean.success("添加成功");
        }else {
            return RespBean.error("添加失败");
        }
    }

    /**
     * 取消选择物流公司
     * @param id
     * @param storeId
     * @return
     */
    @Override
    public RespBean cancelStoreLogistics(Long id, Long storeId) {
        boolean remove = this.remove(new QueryWrapper<StoreLogisticsEntity>()
                .eq("logistics_id", id)
                .eq("store_id", storeId));
        if (remove){
            return RespBean.success("移除成功");
        }else {
            return RespBean.error("移除失败");
        }
    }

}